package com.talkweb.newproject.admin.sysresource.service;

import com.talkweb.newproject.admin.sysmenu.dto.SysMenuDTO;
import com.talkweb.newproject.admin.sysresource.dao.SysResourceDao;
import com.talkweb.newproject.admin.sysresource.dto.SysResourceDTO;
import com.talkweb.newproject.api.model.admin.SysResourceParam;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qsf on 2019/4/19.
 */
@Service
public class SysResourceService implements Serializable {
    @Autowired
    private SysResourceDao sysResourceDao;


    /**
     * 新增
     */
    @Transactional(rollbackFor = {Exception.class})
    public Long insertSysResource(SysResourceDTO dto) throws Exception {
        Map<String,Object> params = new HashMap();
        params.put("dto",dto);
        int count = sysResourceDao.insertSysResource(params);
        SysMenuDTO result = (SysMenuDTO) params.get("dto");
        return result.getId();
    }

    /**
     * 修改资源信息
     */
    @Transactional(rollbackFor = {Exception.class})
    public void updateSysResource(SysResourceDTO sysResourceDTO) throws Exception{
        Map<String,Object> params = new HashMap();
        params.put("dto",sysResourceDTO);
        sysResourceDao.updateSysResource(params);
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteSysResourceById(String id) throws Exception {
        int count = sysResourceDao.deleteSysResourceById(id);
        return count >0;
    }

    /**
     * 根据主键查询资源信息
     */
    public SysResourceDTO querySysResourceByPrimaryKey(String id) throws Exception {
        SysResourceDTO result = sysResourceDao.querySysResourceByPrimaryKey(id);
        if(result== null) {result = new SysResourceDTO();}
        return result;
    }

    /**
     * 根据userid查询shiro权限
     * @param sysResourceDTO
     * @return
     * @throws Exception
     */
    public List<SysResourceDTO> selectShiroUrlPermissionByUserId(SysResourceDTO sysResourceDTO) throws Exception{
        Map<String,Object> params = new HashMap();
        params.put("dto",sysResourceDTO);
        return sysResourceDao.selectShiroUrlPermissionByUserId(params);
    }

    public List<SysResourceDTO> searchSysResource(SysResourceParam obj) throws Exception {
        Map<String,Object> params = new HashMap();
        params.put("dto",obj);
        return sysResourceDao.searchSysResource(params);
    }

    public SysResourceDTO queryLikeSysResource(SysResourceDTO dto) throws Exception{
        Map<String,Object> params = new HashMap<>();
        params.put("dto",dto);
        SysResourceDTO result = sysResourceDao.queryLikeSysResource(params);
        return result;
    }
}
